package com.yuntsg.nnsfcp.service.front.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuntsg.nnsfcp.front.entity.ParsingTxt;
import com.yuntsg.nnsfcp.front.entity.Searchfile;
import com.yuntsg.nnsfcp.front.entity.Searchinfo;
import com.yuntsg.nnsfcp.front.mapper.ParsingTxtMapper;
import com.yuntsg.nnsfcp.front.mapper.SearchfileMapper;
import com.yuntsg.nnsfcp.service.front.SearchFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 纯文本解析实现类
 *
 * @author ：ljx
 * @date ：Created in 2019/6/11
 */
@Slf4j
@Service
public class SearchFileServiceImpl extends ServiceImpl<SearchfileMapper, Searchfile> implements SearchFileService {

    @Resource
    private ParsingTxtMapper parsingTxtMapper;

    @Resource
    private SearchfileMapper searchfileMapper;

    /**
     * fid 记录解析到了哪一个file_id
     */
    private List<Integer> fids = null;

    public List<Integer> getFids() {
        return this.fids;
    }

    /**
     * 解析文本
     *
     * @param searchinfo 待检索列表
     * @param boo        true 解析wos的总共被引次数,false 用户选择的
     * @return
     */
    @Override
    public List<Integer> parsing(Searchinfo searchinfo, boolean boo) {
        //查询状态为待解析的
        List<Searchfile> searchFileList
                = searchfileMapper.selectList(new QueryWrapper<Searchfile>()
                .eq("sid", searchinfo.getSid())
                .eq("txt_status", 0));

        if (searchFileList.size() > 0) {
            fids = new ArrayList<>();
            log.info("4: 解析文本中..");
            for (Searchfile searchfile : searchFileList) {
                //记录id
                fids.add(searchfile.getFid());

                searchfile.setTxtStatus(1);
                searchfileMapper.updateById(searchfile);
                List<ParsingTxt> parsingTxts
                        = runParsing(searchfile.getFid(), searchfile.getCitedtext());
                if (parsingTxts.size() > 0) {
                    for (ParsingTxt parsingTxt : parsingTxts) {
                        parsingTxtMapper.insert(parsingTxt);
                    }
                }
            }
        }
        searchFileList = null;
        return fids;
    }

    /**
     * 运行解析
     *
     * @param fid    张哥表中的主键
     * @param txtStr 网页下载下来的纯文本
     * @return
     */
    @Override
    public List<ParsingTxt> runParsing(Integer fid, String txtStr) {
        //封装数据返回
        ArrayList<ParsingTxt> parsingTxts = new ArrayList<>();
        //判空
        if (txtStr != null && txtStr.length() > 0) {
            if (!txtStr.contains("<!DOCTYPE html>")) {
                //按照 \n 解析一行
                String[] line = txtStr.split("\n");
                //0为首行字段, PT AU.... 故从第1行开始取数据
                for (int i = 1; i < line.length; i++) {
                    //再解析一行中的 \t 切分为多个
                    String[] split = line[i].split("\t");
                    //判断到解析到的数据是否为68个
                    if (split.length - 1 == 68) {
                        //封装数据 SCIE有68
                        ParsingTxt txt = saveTxtScie(split, fid);
                        parsingTxts.add(txt);
                    } else if (split.length - 1 == 42) {
                        //cscd有42
                        ParsingTxt txt = saveTxtCscd(split, fid);
                        parsingTxts.add(txt);
                    }
                }
            }
        }
        return parsingTxts;
    }

    /**
     * 保存 txt 信息
     *
     * @param split 解析切割的数组
     * @param fid   file表id
     */
    private ParsingTxt saveTxtScie(String[] split, Integer fid) {
        //封装数据
        ParsingTxt txt = new ParsingTxt();
        txt.setFileId(fid);
        txt.setPt(split[0]);
        txt.setAu(split[1]);
        txt.setBa(split[2]);
        txt.setBe(split[3]);
        txt.setGp(split[4]);
        txt.setAf(split[5]);
        txt.setBf(split[6]);
        txt.setCa(split[7]);
        txt.setTi(split[8]);
        txt.setSo(split[9]);
        txt.setSe(split[10]);
        txt.setBs(split[11]);
        txt.setLa(split[12]);
        txt.setDt(split[13]);
        txt.setCt(split[14]);
        txt.setCy(split[15]);
        txt.setCl(split[16]);
        txt.setSp(split[17]);
        txt.setHo(split[18]);
        txt.setDe(split[19]);
        txt.setId(split[20]);
        txt.setAb(split[21]);
        txt.setC1(split[22]);
        txt.setRp(split[23]);
        txt.setEm(split[24]);
        txt.setRi(split[25]);
        txt.setOi(split[26]);
        txt.setFu(split[27]);
        txt.setFx(split[28]);
        txt.setCr(split[29]);
        txt.setNr(split[30]);
        txt.setTc(split[31]);
        txt.setZ9(split[32]);
        txt.setU1(split[33]);
        txt.setU2(split[34]);
        txt.setPu(split[35]);
        txt.setPi(split[36]);
        txt.setPa(split[37]);
        txt.setSn(split[38]);
        txt.setEi(split[39]);
        txt.setBn(split[40]);
        txt.setJ9(split[41]);
        txt.setJi(split[42]);
        txt.setPd(split[58]);
        if (StringUtils.isNotEmpty(split[44])) {
            txt.setPy(Integer.valueOf(split[44].trim()));
        }
        txt.setVl(split[45]);
        txt.setIss(split[46]);
        txt.setPn(split[47]);
        txt.setSu(split[48]);
        txt.setSi(split[49]);
        txt.setMa(split[50]);
        txt.setBp(split[51]);
        txt.setEp(split[52]);
        txt.setAr(split[53]);
        txt.setDi(split[54]);
        txt.setD2(split[55]);
        txt.setEa(split[56]);
        txt.setEy(split[57]);
        txt.setPg(split[58]);
        txt.setWc(split[59]);
        txt.setSc(split[60]);
        txt.setGa(split[61]);
        txt.setUt(split[62]);
        txt.setPm(split[63]);
        txt.setOa(split[64]);
        txt.setHc(split[65]);
        txt.setHp(split[66]);
        txt.setDa(split[67]);
        return txt;
    }

    /**
     * 保存CSCD 的数据
     *
     * @param split 解析的数组
     * @param fid   file表id
     * @return
     */
    private ParsingTxt saveTxtCscd(String[] split, Integer fid) {
        ParsingTxt txt = new ParsingTxt();
        txt.setFileId(fid);
        txt.setPt(split[0]);
        txt.setAu(split[1]);
        txt.setZ2(split[2]);
        txt.setTi(split[3]);
        txt.setZ1(split[4]);
        txt.setSo(split[5]);
        txt.setZ3(split[6]);
        txt.setVl(split[7]);
        txt.setIss(split[8]);
        txt.setBp(split[9]);
        txt.setEp(split[10]);
        txt.setAr(split[11]);
        if (StringUtils.isNotEmpty(split[12])) {
            txt.setPy(Integer.valueOf(split[12].trim()));
        }
        txt.setLa(split[13]);
        txt.setDt(split[14]);
        txt.setDe(split[15]);
        txt.setZ5(split[16]);
        txt.setAb(split[17]);
        txt.setZ4(split[18]);
        txt.setC1(split[19]);
        txt.setZ6(split[20]);
        txt.setEm(split[21]);
        txt.setZ7(split[22]);
        txt.setRi(split[23]);
        txt.setOi(split[24]);
        txt.setCr(split[25]);
        txt.setNr(split[26]);
        txt.setZ8(split[27]);
        txt.setZ9(split[28]);
        txt.setU1(split[29]);
        txt.setU2(split[30]);
        txt.setFu(split[31]);
        txt.setSn(split[32]);
        txt.setBn(split[33]);
        txt.setDi(split[34]);
        txt.setPg(split[35]);
        txt.setSc(split[36]);
        txt.setUt(split[37]);
        txt.setOa(split[38]);
        txt.setHc(split[39]);
        txt.setHp(split[40]);
        txt.setDa(split[41]);
        return txt;
    }
}
